import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class TrivialClassifier implements Classifier {
    SearchEngine engine;
    BookTrie trie;

    public void setSearchEngine(SearchEngine engine) {
        this.engine = engine;
    }

    public void setTrie(BookTrie trie) {
        this.trie = trie;
    }

    public ClassifierDecisionInfo makeDecision(String query) throws Exception {
        List<WebDocument> docs = engine.search(query);

        ClassifierDecisionInfo info = new ClassifierDecisionInfo();
        HashMap<Integer,WebDocument.MentionedBook> comments = new HashMap<Integer,WebDocument.MentionedBook>();

        for(WebDocument doc : docs) {
            List<WebDocument.MentionedBook> relatedBooks = doc.getRelatedBooks(trie);
            for(WebDocument.MentionedBook book : relatedBooks) {
                if (book.isWithAuthor) {
                    info.isBookRelatedQuery = true;
                    comments.put(book.bookId, book);
                }
            }
        }

        for(WebDocument.MentionedBook book : comments.values()) {
            info.comments.add(
                "\"" + StringUtils.join(book.title," ") + "\" (" + StringUtils.join(book.authors," ") + ")"
            );
        }

        return info;
    }
}
